import pymysql
import SimpleMySqlClass as smc

# 主函数main对类进行测试
if __name__ == "__main__":
    # 对类实例化
    host = "127.0.0.1"
    mysql_obj = smc.SimpleMySqlClass(host, "stocks", "haojg", "haojg")
    # 有一个数据库名字是test_db，该数据库下有一张表user_info
    # 调用类中的函数query_sql执行查询语句
    re = mysql_obj.query_sql("select * from user_info")
    print(re)
    # 打印结果：(('xiaoxiao', 1), ('xiaoA', 2), ('xiaoB', 3), ('xiaoC', 4), ('xiaoD', 5))
    """
    row的打印结果：
    ('anle', 1)
    ('xiaoA', 2)
    ('xiaoB', 3)
    ('xiaoC', 4)
    ('xiaoD', 5)
    """
    for row in re:
        print("序号:", row[1], "姓名:", row[0])

    # 执行增删改的SQL语句
    add_count = mysql_obj.execute('insert into user_info values("xiaoE",6)')
    if add_count:
        print("添加成功")
    else:
        print("添加失败")

    del_count = mysql_obj.execute('delete from user_info where name = "xiaoE"')
    if del_count:
        print("删除成功")
    else:
        print("删除失败")

    # 列举数据库中的表：
    table_names = (
        mysql_obj.list_tables()
    )  # 打印table_names是一个元组，(('user',), ('user_info',))
    # print(table_names)
    for table in table_names:
        print(table[0])
    # 关闭数据库连接，节省计算机资源
    mysql_obj.close()
